/* eslint-disable no-dupe-class-members */
'use strict';

const BaseService = require('./base');

class QuesttionAnswerService extends BaseService {
  async add(params) {
    return this.run(async () => {
      const { ctx } = this;
      const result = await ctx.model.QuesttionAnswer.create(params);
      return result;
    });
  }

  async query() {
    return this.run(async () => {
      const { ctx, app } = this;
      const Op = app.Sequelize.Op;
      return await ctx.model.QuesttionAnswer.findAll({
        where: {
          delete_time: {
            [Op.is]: null,
          },
        },
        order: [
          [ 'create_time', 'desc' ],
        ],
      });
    });
  }

  async delete(id, delete_time) {
    return this.run(async () => {
      const { ctx } = this;
      const deleteValue = await ctx.model.QuesttionAnswer.findOne({
        where: {
          id,
        },
        attributes: [ 'delete_time' ],
      });
      !deleteValue && ctx.helper.commonErr('未查询到数据', {});

      // 判断数据是否已删除
      (deleteValue.dataValues.delete_time) && ctx.helper.commonErr('请勿重复删除', {});
      const result = await ctx.model.QuesttionAnswer.update({ delete_time }, {
        where: {
          id,
        },
      });
      return result;
    });
  }
}

module.exports = QuesttionAnswerService;
